package com.polaris.live.dialog.common

import android.content.Context
import android.view.View
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.databinding.DialogConfirmCanalDetailBinding
import com.polaris.live.im.sfs.bean.chat.ext.RouterHighlight
import com.polaris.live.utils.route.RouterUtils

/**
 * ConfirmCanalDetailDialog
 *
 * @author Created by 半仙 on 2023/11/21/021 14:32
 */
class ConfirmCanalDetailDialog(
    context: Context,
    private val mTitle: String,
    private val mDetail: String,
    private val mConfirm: String? = null,
    private val mCanal: String? = null,
    private val highlight: List<RouterHighlight>? = null,
    private val onConfirmCallback: (() -> Unit)? = null,
    private val onCanalCallback: (() -> Unit)? = null,
    private val textIsCenter: Boolean? = null,
) : CenterPopupView(context) {

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_confirm_canal_detail
    }

    private lateinit var mBinding: DialogConfirmCanalDetailBinding

    override fun onCreate() {
        super.onCreate()
        mBinding = DialogConfirmCanalDetailBinding.bind(popupImplView)

        mBinding.apply {
            tvTitle.text = mTitle

            if (highlight != null) {
                RouterUtils.handleText(tvDetail, mDetail, highlight)
            } else {
                tvDetail.text = mDetail
            }

            if (mConfirm == null) {
                tvConfirm.visibility = View.GONE
            } else {
                tvConfirm.text = mConfirm
                tvConfirm.visibility = View.VISIBLE
            }

            if (mCanal == null) {
                tvCancel.visibility = View.GONE
            } else {
                tvCancel.visibility = View.VISIBLE
                tvCancel.text = mCanal
            }

            if (textIsCenter == true) {
                tvDetail.textAlignment = TEXT_ALIGNMENT_CENTER
            }

            tvConfirm.setOnSingleClickListener {
                onConfirmCallback?.invoke()
                dismiss()
            }

            tvCancel.setOnSingleClickListener {
                onCanalCallback?.invoke()
                dismiss()
            }
        }
    }

    companion object {
        fun show(
            context: Context,
            mTitle: String,
            mDetail: String,
            mConfirm: String? = null,
            mCanal: String? = null,
            highlight: List<RouterHighlight>? = null,
            onConfirmCallback: (() -> Unit)? = null,
            onCanalCallback: (() -> Unit)? = null,
            textIsCenter: Boolean? = null,
        ) {
            XPopup.Builder(context)
                .isDestroyOnDismiss(true)
                .asCustom(
                    ConfirmCanalDetailDialog(
                        context,
                        mTitle,
                        mDetail,
                        mConfirm,
                        mCanal,
                        highlight,
                        onConfirmCallback,
                        onCanalCallback,
                        textIsCenter
                    )
                )
                .show()
        }
    }
}